/* Copyright 2009 Palm, Inc.  All rights reserved. */

var TelephonyService = Class.create({ 
	initialize: function() { 
	}
});


TelephonyService.identifier = 'palm://com.palm.telephony';

TelephonyService.phoneNumberQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'phoneNumberQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.simbookRead = function(limit, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'simbookRead',
		parameters: {
	        'index': 0,
	        'type': "fdn",
	        'indexEnd': limit
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.simbookCapacityQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'simbookCapacityQuery',
		parameters: {
	    	'type': "fdn"
		},
		onSuccess: callback,
		onFailure: callback
	});
    return request;    
}

TelephonyService.simbookEntryMaxLengthQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'simbookEntryMaxLengthQuery',
		parameters: {
		    'type': "fdn"
		},
		onSuccess: callback,
		onFailure: callback
	});
    return request;    
}

TelephonyService.powerQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'powerQuery',
		parameters: {subscribe: true},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.powerSet = function(state,callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'powerSet',
		parameters: {
			'state': state
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.platformQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'platformQuery',
		onSuccess: function(response) {
					//Settings.platform = (response.extended.platformType);
                	//Mojo.Controller.stageController.delegateToSceneAssistant("doPlatformSpecificSetup");
					callback(response);
				 },
		onFailure: function(response){
				
				setTimeout(function(){
					TelephonyService.platformQuery()
				}, 5000);
		}
	});
	return request;
}

TelephonyService.simbookWrite = function(callback, type, name, number, index){
	var params;
	if (index!==undefined)	
		params = {
			'index': index,
			'name': name,
			'number':number,
			'type': type
		}
	else
		params = {
			'name': name,
			'number':number,
			'type': type
		}
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'simbookWrite',
		parameters: params,
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.simbookDelete = function(index, type, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'simbookDelete',
		parameters: {
			'index': index,
			'type': type
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.ttyQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "ttyQuery",
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.ttySet = function(mode, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "ttySet",
		parameters: {
    		"mode": mode
		},
		onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
		onFailure: function(response) {
					if(callback) 
						callback(response)
					},
	});
	return request;
}

TelephonyService.hacQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "hacQuery",
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.hacSet = function(state, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "hacSet",
		parameters: {
    		"enable": state
		},
		onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
		onFailure: function(response) {
					if(callback) 
						callback(response)
					},
	});
	return request;
}

TelephonyService.ratSet = function(mode, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "ratSet",
		parameters: {
    		"mode": mode
		},
		onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
		onFailure: function(response) {
					if(callback) 
						callback(response)
					},
	});
	return request;
}

TelephonyService.ratQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "ratQuery",
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.clirQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'clirQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.clirSet = function(status, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'clirSet',
		parameters: {
			'restrict': status
		},
		onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
		onFailure: function(response) {
					if(callback) 
						callback(response)
					},
	});
	return request;
}

TelephonyService.dtmfDurationQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'dtmfDurationQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.dtmfDurationSet = function(status, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'dtmfDurationSet',
		parameters: {
		    'dtmflong': status
		},
		onSuccess: function(response) {
					if(callback)
						callback(response);
			},
		onFailure: function(response) {
					if(callback)
						callback(response);
			},
	});
	return request;
}

TelephonyService.roamModeQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'roamModeQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}
    
TelephonyService.roamModeSet = function(modeValue, band, callback){
	
	var params = {
		'mode': modeValue
	}
	
	if (band) {
		params.band = band;
	}
	
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'roamModeSet',
		"parameters": params,
		onSuccess: function(response) {
					if(callback)
						callback(response);
			},
		onFailure: function(response) {
					if(callback)
						callback(response);
			},
	});
	return request;
}

TelephonyService.networkListQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkListQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.networkListQueryCancel = function(){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkListQueryCancel',
		onSuccess: function() {
			Mojo.Log.info("cancelled network list query");
		},
		onFailure: function() {
			Mojo.Log.info("failed cancelled network list query (possibly normal)");
		}
	});
	return request;
}

TelephonyService.networkSelectionModeQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkSelectionModeQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

// used to hide manual network selection if not permitted by SIM card
TelephonyService.manualNetworkSelectionPermittedQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'manualNetworkSelectionPermittedQuery',
		parameters: { 
			'subscribe':true
		},
		onSuccess: function(payload) {
			var enabled = true;
			if (payload && payload.extended) {
				enabled = payload.extended.enabled;
			} 
			
 			callback(enabled);
		},
		onFailure: function() {
			Mojo.Log.error("manualNetworkSelectionPermittedQuery failed")
			callback(true);
		}
	});
	return request;
}


TelephonyService.networkSet = function(status, id,callback){
	var params;
	if (status !== undefined)
		params = {"automatic": status, "id": id};
	else if (id != undefined)
		params = {"automatic": false,"id": id};
	else
		params = {"automatic": true};
		
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkSet',
		parameters: params,
		onSuccess: function(response) {
					if(callback) 
						callback(true)
					},
		onFailure: function(response) {
					if(callback) 
						callback(false)
					}
	});
	return request;
}

    
TelephonyService.networkIdQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkIdQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.callWaitingQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'callWaitingQuery',
		parameters: {
		    "bearer": "defaultbearer"
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.callWaitingSet = function(status, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'callWaitingSet',
		parameters: {
		    "bearer": "defaultbearer",
		    "enable": status
		},
		onSuccess: function(response) {
					if(callback) 
						callback(response)
					},
		onFailure: function(response) {
					if(callback) 
						callback(response)
					},
	});
	return request;
}
    
TelephonyService.forwardQuery = function(key, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'forwardQuery',
		parameters: {
		    "condition": key,
		    "bearer": "defaultbearer"
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}
    
TelephonyService.forwardRegister = function(key, number, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'forwardRegister',
		parameters: {
			"condition": key,
			"bearer": "defaultbearer",
			"time": "0",
			"number": number
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}
    
TelephonyService.pin1Status = function(subscribe, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin1Status',
		parameters:{"subscribe":subscribe},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.pin2Status = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin2Status',
		parameters:{"subscribe":true},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.setPinStatus = function(pin, status, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: ((status == true) ? 'pin1Enable' : 'pin1Disable'),
		parameters: {
			"pin": pin
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.unlockTelephony = function(pin, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'unlockTelephony',
		parameters: {
			"pin": pin
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.pin1Change = function(oldPin, newPin, success, failure){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin1Change',
		parameters: {
			"oldPin": oldPin,
			"newPin": newPin
		},
		onSuccess: success,
		onFailure: failure
	});
	return request;
}

TelephonyService.pin1Reset = function(puk, pin, pinconf, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin1Reset',
		parameters: {
			"puk": puk,
			"newPin": pin,
			"newPinConfirm": pinconf
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.pin2Reset = function(puk, pin, pinconf, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin2Reset',
		parameters: {
			"puk2": puk,
			"newPin2": pin,
			"newPinConfirm": pinconf
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.pin2Verify = function(pin, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin2Verify',
		parameters: {
			"pin2": pin
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.pin2Change = function(oldPin, newPin, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'pin2Change',
		parameters: {
			"oldPin": oldPin,
			"newPin": newPin
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.fixedDialingQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: "fixedDialingQuery",
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.setFixedDialingStatus = function(pin, status, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: ((status == true) ? 'fixedDialingEnable' : 'fixedDialingDisable'),
		parameters: {
			"pin2": pin
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.networkStatusQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'networkStatusQuery',
		parameters: {subscribe: true},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.subscribePower = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'subscribe',
		parameters: {
			'events': 'power'
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.subscribeSecurity = function(callback){
        var request = new Mojo.Service.Request(TelephonyService.identifier, {
            method: 'subscribe',
            parameters: {
                'events': 'security'
            },
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.voicemailNumberQuery = function(callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'voicemailNumberQuery',
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.voicemailNumberQueryCarrierDb = function(callback) {
	var request = new Mojo.Service.Request('palm://com.palm.data.carriernetworksettings', {
          	method: 'getCurrentSettings',
			parameters:{'settings':['voicemailNumber'],"subscribe":true},
            onSuccess: function(response){
                callback(true, response.voicemailNumber)
            },
            onFailure: function(response){
                callback(false, (Messages.generalServiceError))
            }
        });
		
	return request;
}

TelephonyService.voicemailNumberSet = function(number, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'voicemailNumberSet',
		parameters: {
			'number': number
		},
		onSuccess: callback,
		onFailure: callback
	});
	return request;
}

TelephonyService.voicemailReset = function(){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'voicemailCountReset',	
	});
	return request;
}

TelephonyService.deviceLockUnlock = function(pin, type, unblock, callback){
	var request = new Mojo.Service.Request(TelephonyService.identifier, {
		method: 'deviceLockUnlock',
		parameters: {
			'type': type,
			'key': pin,
			'unblock': unblock
		},
		onSuccess: callback,
		onFailure: callback,
	});
	return request;
}

